package com.sddm.printServer.service.impl;

import cn.hutool.json.JSONObject;
import com.sddm.printServer.entiy.Print;
import com.sddm.printServer.service.PrintProcessDataAdapter;
import org.springframework.stereotype.Service;

import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.PageRanges;
import javax.print.attribute.standard.Sides;

/**
 * @author wyh
 * @version 1.0
 * @date 2022/4/7 13:37
 */
@Service
public class PrintProcessDataAdapterImpl implements PrintProcessDataAdapter {

    @Override
    public HashPrintRequestAttributeSet printProcessData(Print print) {
        //添加打印属性
        HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();

        if (print.getPageRanges() != null) {
            String pageRanges = print.getPageRanges();
            // 打印范围，第几页到第几页
            pars.add(new PageRanges(Integer.parseInt(pageRanges.split(",")[0]),
                    Integer.parseInt(pageRanges.split(",")[1])));
        }

        // 打印份数
        if (print.getCopies() != null) {
            // 打印范围，第几页到第几页
            pars.add(new Copies(print.getCopies()));
        }

        // 纸张大小
        if (print.getMediaSize() != null) {
            String mediaSize = print.getMediaSize();
            switch (mediaSize) {
                case "A4":
                    pars.add(MediaSizeName.ISO_A4);
                    break;
                case "A3":
                    pars.add(MediaSizeName.ISO_A3);
                    break;
                case "A5":
                    pars.add(MediaSizeName.ISO_A5);
                    break;
                case "B4":
                    pars.add(MediaSizeName.ISO_B4);
                    break;
                default:
                    pars.add(MediaSizeName.ISO_A4);
                    break;
            }
        }

        if (print.getSides() != null) {
            if (print.getSides()) {
                //设置双面
                pars.add(Sides.DUPLEX);
            }
        }

        return pars;
    }
}
